package com.ronghui.cash.config;

import com.ronghui.cash.util.IdWorker;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.baomidou.mybatisplus.MybatisConfiguration;
import com.baomidou.mybatisplus.MybatisXMLLanguageDriver;
import com.baomidou.mybatisplus.entity.GlobalConfiguration;
import com.baomidou.mybatisplus.enums.DBType;
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.boot.autoconfigure.MybatisProperties;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import redis.clients.jedis.JedisPoolConfig;

import javax.sql.DataSource;

@Configuration
public class Config {

        @Autowired
        private DataSource dataSource;

        @Autowired
        private MybatisProperties properties;

        @Autowired
        private ResourceLoader resourceLoader = new DefaultResourceLoader();

        @Autowired(required = false)
        private Interceptor[] interceptors;

        @Autowired(required = false)
        private DatabaseIdProvider databaseIdProvider;

    //    @Value("workerId")
        private int workerId=0;

        @Bean
        public IdWorker createIdWorker(){
            return new IdWorker(workerId);
        }

        @Bean
        public BCryptPasswordEncoder createBCryptPasswordEncoder(){
            return new BCryptPasswordEncoder();
        }

        /**
         * mybatis-plus分页插件
         */
        @Bean
        public PaginationInterceptor paginationInterceptor() {
            PaginationInterceptor page = new PaginationInterceptor();
            page.setDialectType("mysql");
            return page;

        }

        /**
         * 这里全部使用mybatis-autoconfigure 已经自动加载的资源。不手动指定 配置文件和mybatis-boot的配置文件同步
         *
         * @return
         */
        @Bean
        public MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean() {
            MybatisSqlSessionFactoryBean mybatisPlus = new MybatisSqlSessionFactoryBean();
            mybatisPlus.setDataSource(dataSource);
            mybatisPlus.setVfs(SpringBootVFS.class);
            if (StringUtils.hasText(this.properties.getConfigLocation())) {
                mybatisPlus.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
            }
            mybatisPlus.setConfiguration(properties.getConfiguration());
            if (!ObjectUtils.isEmpty(this.interceptors)) {
                mybatisPlus.setPlugins(this.interceptors);
            }
            // MP 全局配置，更多内容进入类看注释
            GlobalConfiguration globalConfig = new GlobalConfiguration();
            globalConfig.setDbType(DBType.MYSQL.name());
            // ID 策略 AUTO->`0`("数据库ID自增")
            // INPUT->`1`(用户输入ID") ID_WORKER->`2`("全局唯一ID") UUID->`3`("全局唯一ID")
            globalConfig.setIdType(1);
            mybatisPlus.setGlobalConfig(globalConfig);
            MybatisConfiguration mc = new MybatisConfiguration();
            mc.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
            mc.setMapUnderscoreToCamelCase(true);
            mc.setUseColumnLabel(true);
            mybatisPlus.setConfiguration(mc);
            if (this.databaseIdProvider != null) {
                mybatisPlus.setDatabaseIdProvider(this.databaseIdProvider);
            }
            if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) {
                mybatisPlus.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
            }
            if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {
                mybatisPlus.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
            }
            if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) {
                mybatisPlus.setMapperLocations(this.properties.resolveMapperLocations());
            }
            return mybatisPlus;
        }

        /**
         * redis的功能
         *
         * @return
         */
        @Bean
        @ConfigurationProperties(prefix = "spring.redis")
        public JedisPoolConfig getRedisConfig() {
            JedisPoolConfig config = new JedisPoolConfig();
            return config;
        }

        @Bean
        @ConfigurationProperties(prefix = "spring.redis")
        public JedisConnectionFactory getConnectionFactory() {
            JedisConnectionFactory factory = new JedisConnectionFactory();
            JedisPoolConfig config = getRedisConfig();
            factory.setPoolConfig(config);
            return factory;
        }

        @Bean
        public RedisTemplate<?, ?> getRedisTemplate() {
            RedisTemplate<?, ?> template = new StringRedisTemplate(getConnectionFactory());
            return template;
        }

}
